import Taro from '@tarojs/taro'

export function standardUnitConvert(
  str: string
): StandardUnit | StandardCnUnit | undefined {
  switch (str) {
    case '大于等于':
      return '>='
    case '大于':
      return '>'
    case '小于等于':
      return '<='
    case '小于':
      return '<'
    case '两者之间':
      return '-'
    case '>=':
      return '大于等于'
    case '>':
      return '大于'
    case '<=':
      return '小于等于'
    case '<':
      return '小于'
    case '-':
      return '两者之间'
    default:
      return undefined
  }
}

export function standardUnit2Cn(unit: string): StandardCnUnit | undefined {
  switch (unit) {
    case '>=':
      return '大于等于'
    case '>':
      return '大于'
    case '<=':
      return '小于等于'
    case '<':
      return '小于'
    case '-':
      return '两者之间'
    default:
      return undefined
  }
}

export function standardCn2Unit(cn: string): StandardUnit | undefined {
  switch (cn) {
    case '大于等于':
      return '>='
    case '大于':
      return '>'
    case '小于等于':
      return '<='
    case '小于':
      return '<'
    case '两者之间':
      return '-'
    default:
      return undefined
  }
}

export function splitStandardUnit(stand: string): StandardUnit | undefined {
  let reg = /\S*(\>=|\>|\<=|\<|\-)\S*/
  if (reg.test(stand)) {
    let unit = stand.replace(reg, '$1')
    return unit as StandardUnit
  } else {
    return undefined
  }
}

export function splitStandardCnUnit(stand: string): StandardCnUnit | undefined {
  let reg = /\S*('大于等于'| '大于'| '小于'| '小于等于'| '两者之间')\S*/
  if (reg.test(stand)) {
    let cnUnit = stand.replace(reg, '$1')
    return cnUnit as StandardCnUnit
  } else {
    return undefined
  }
}

export function checkParamStandardIsNull(
  standard: string | undefined | null
): boolean {
  if (standard === 'N/A' || !standard) return true
  return false
}

/**校验合格标准字符串是否包含数值 */
export function checkParamStandard(standard: string): boolean {
  if (checkParamStandardIsNull(standard)) {
    Taro.showToast({title: '请选择合格范围！', duration: 1000})
    return false
  }
  console.log(1)

  let bewteen = standard.indexOf('-') !== -1
  let big = standard.indexOf('>=') !== -1
  let small = standard.indexOf('<=') !== -1

  console.log(2, bewteen, big, small)
  if (bewteen || big || small) {
    standard = standard.replace(/\>=|\<=|\-/g, '$')
    let valueList = standard.split('$')
    console.log(3, valueList)

    if (valueList.length !== 2) return false
    for (let i = 0; i < valueList.length; i++) {
      const val = valueList[i]
      if (bewteen) {
        if (!val) {
          Taro.showToast({title: '请完善范围填写！', duration: 1000})
          return false
        }
      } else {
        if (!val && i === 1) {
          Taro.showToast({title: '请完善范围填写！', duration: 1000})
          return false
        }
      }
    }
    return true
  } else {
    return false
  }
}
